<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	function __construct() {
		parent::__construct();
	}
	
	public function register(){
		if(!$_POST){
			$this->load->view('user/register.php');
			return;
		}
		
		$user = $this->input->get_post('user');
		$vcode = $this->input->get_post('vercode');
		
		$this->load->model('common/Captcha_model');
		$isValid = $this->Captcha_model->validate($vcode);
		if(!$isValid){
			$data['errorMsg'] = $this->Resultmessage_model->getMessage('VCODE_ERROR');
			$data['formInfo'] = $user;
			$this->load->view('user/register.php', $data);
			return;
		}
		
		$this->load->model('users/Passport_model');
		$result = $this->Passport_model->register($user);
		
		redirect('/');
	}
	
	public function login(){
		$returnUrl = $this->input->get('returnUrl');
		$data = array(
			'returnUrl'=> empty($returnUrl) ? '/' : $returnUrl 
		);
		
		if(!$_POST){
			$this->load->view('user/login.php', $data);
			return;
		}

		$user = $this->input->get_post('user');
		$vcode = $this->input->get_post('vercode');

		$this->load->model('common/Captcha_model');
		$isValid = $this->Captcha_model->validate($vcode);
		if(!$isValid){
			$data['errorMsg'] = $this->Resultmessage_model->getMessage('VCODE_ERROR');
			$data['formInfo'] = $user;
			$this->load->view('user/login.php', $data);
			return;
		}
		
		$this->load->model('users/Passport_model');
		$result = $this->Passport_model->login($user);
		if($result['ret'] != 0){
			$data['errorMsg'] = $result['msg'];
			$data['formInfo'] = $user;
			$this->load->view('user/login.php', $data);
			return;
		}

		redirect($returnUrl);
	}
	
	public function logout(){
		$this->load->model('users/Passport_model');
		$this->Passport_model->logout();
		
		redirect('/');
	}
	
	public function genVcode(){
		$this->load->model('common/Captcha_model');
		$this->Captcha_model->gen();
	}

	public function agreement(){
		$this->load->view('user/agreement');
	}
	
	public function forgetPassword(){
		$this->load->view('user/forgetPassword');
	}
	
	public function findPassword(){
			
	}
	
	public function appList(){
		$this->load->view('testList');
	}
}